package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.vo.SkuLockVo;
import com.spzx.product.service.SkuStockService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.utils.ExceptionUtil;

import java.util.List;

/**
 * <p>
 * 商品sku库存表 前端控制器
 * </p>
 *
 * @author Haoran Sun
 * @since 2024-07-15
 */
@RestController
@RequestMapping("/skuStock")
public class SkuStockController extends BaseController {

    @Resource
    private SkuStockService skuStockService;

    /**
     * 检查并锁定库存
     * @param orderNo 订单号
     * @param skuLockVoList 商品要锁定的库存信息
     * @return 有误则返回错误信息，无误则返回空字符串
     */
    @InnerAuth
    @Operation(summary = "检查与锁定库存")
    @PostMapping("checkAndLock/{orderNo}")
    public R<String> checkAndLock(@PathVariable String orderNo, @RequestBody List<SkuLockVo> skuLockVoList) {
        try {
            return R.ok(skuStockService.checkAndLock(orderNo, skuLockVoList));
        } catch (ServiceException e) {
            logger.error("{}",ExceptionUtil.getExceptionMessage(e));
            return R.ok(e.getMessage());
        } catch (Exception e) {
            logger.error("库存不足:{}",ExceptionUtil.getExceptionMessage(e));
            return R.ok("库存不足");
        }
    }

}
